/**
 *
 * The MIT License
 *
 * Copyright (c) 2009 Noritaka Horio All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 * 
 * @author Noritaka Horio 
 * @copyright Copyright (c) 2009 Noritaka Horio All rights reserved.
 * @license The MIT License
 * @version $Rev: 187 $ $Date: 2009-11-08 03:25:12 +0000 (Sun, 08 Nov 2009) $
 * @see http://mootools.net
 * 
 */

var Clock = {

	initialize: function() {
		this.api = parentSandboxBridge;
		this.container = $("warpper");
		this.title = $(this.container).getElement(".title");
		this.dateTime = $(this.container).getElement(".dateTime");
		this.tasks = [];
		this.setStartHandler();
		this.loadClockTasks();
		this.buildStructure();
		this.setEvents();
	},

	loadClockTasks: function() {
		var context = {
			"title": this.title,
			"dateTime": this.dateTime,
			"api": this.api
		};
		var tasks = this.api.getClockTasks();
		for (var i = 0; l = tasks.length, i < l; i++) {
			var task = null; 
			var properties = tasks[i];
			try {
				var source = this.api.getClockPlugin(properties.plugin + ".js");
				var handler = eval(source);
				handler = handler.bind(context);
				switch(properties.type) {
					case Task.Types.STATIC:
						task = new Task.Static(properties.value, handler);
						break;
					case Task.Types.PERIODICAL:
					default:
						task = new Task.Periodical(properties.value, handler);
						break;
				}
				this.tasks.push(task);
			} catch(e) {
				this.api.trace(e.message);
			}
		}
	},

	setStartHandler: function() {
		var context = {"title": this.title, "dateTime": this.dateTime, "api": this.api};
		var source = this.api.getClockPlugin("processing.js");
		var handler = eval(source);
		this.startHandler = handler.bind(context);
	},

	start: function(){
		this.dateTime.set("html", this.getDate() + " " + this.getTime());
		return this.startHandler();
	},

	checkTime: function() {
		this.dateTime.set("html", this.getDate() + " " + this.getTime());

		var tasks = this.tasks;
		for (var i = 0; l = tasks.length, i < l; i++) {
			try {
				var task = tasks[i];
				var result = task.execute();
				if (task.getType() == Task.Types.STATIC) {
					tasks.erase(task);
				}
			} catch(e) {
this.api.trace(e.message);
			}

			this.execute(result);
		}
	},

	execute: function(result) {
		if (result) {
			if (result.code) {
				var images = [];
				if (result.images) {
					for (var i = 0; i < result.images.length; i++) {
						images.push([result.images[i], result.images[i]]);
					}
				}
				(images.length > 0) ? this.processing.run(result.code, images)
					: this.processing.run(result.code);
			}
		}
	},

	getTime: function(){
		var date = new Date();
		var h = date.getHours(), m = date.getMinutes();
		var hourValue = (h >= 10) ? String(h) : "0" + String(h);
		var minuteValue = (m >= 10) ? String(m) : "0" + String(m);
		var timeFormat = hourValue + ":" + minuteValue;
		return timeFormat;
	},

	getDate: function () {
		var date = new Date();
		var year = date.getFullYear();
		var month = date.getMonth() + 1;
		var day = date.getDate();
		var dateFormat = year + "-" + month + "-" + day;
		return dateFormat;
	},

	buildStructure: function() {
		this.processing = new Element("object", {
			"id": "emulator",
			"type": "application/x-shockwave-flash",
			"width": "0",
			"height": "0",
			"data": "app:/libraries/processing/processing.swf"
		});
		var param = new Element("param", {
			"name": "allowScriptAccess",
			"value": "always"
		});
		param.inject(this.processing);
		this.processing.inject($("processing"));
	},

	setEvents: function() {
		ProcessingAS.onLoad = function () {
			this.processing.start();
			this.checkTime.periodical(60000, this);
			this.execute(this.start());
		}.bind(this)

		ProcessingAS.onResize = function (w, h) {
			this.processing.width = w;
			this.processing.height = h;
		}.bind(this)
	}

}

window.addEvent("domready", Clock.initialize.bind(Clock));
